﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebSite.Common.Interfaces.Repositories;
using WebSite.Common.Models.ViewModels;

namespace WebSite.Repositories
{
    public class PageDataDbRepository : IPageDataRepository
    {
        private IMainPageItemsRepository _mainPageRepository;
        private ICourseGroupRepository   _courseGroupRepository;
        private ILayoutRepository        _layoutRepository; 

        public PageDataDbRepository(IMainPageItemsRepository mpRepo,ICourseGroupRepository cgRepo,ILayoutRepository layoutRepo)
        {
            _mainPageRepository    = mpRepo;
            _courseGroupRepository = cgRepo;
            _layoutRepository      = layoutRepo;
        }

        public Models.MainPageVM LoadMainPageData()
        {
            Models.MainPageVM result                = new Models.MainPageVM();
            IEnumerable<MainPageItemVM> repoResult = _mainPageRepository.LoadUiData();

            PageLayoutVM pageLayout = _layoutRepository.LoadLayout("main");

            result.Layout = pageLayout;

            foreach (MainPageItemVM i in repoResult)
            {
                result.AddItem(new Models.MainPageVM.MainPageItemVM()
                {
                    DestinationAction = i.DestinationAction,
                    DestinationActionParams = i.DestinationActionParams,
                    DestinationController = i.DestinationController,
                    ImgUrl = i.ImgUrl,
                    ItemCss = i.ItemCss,
                    ItemText = i.ItemText,
                    ItemTitle = i.ItemTitle
                });
            }
            
            return result;
        }

        public Models.TrainingPageVM LoadTrainingPageData()
        {
            Models.TrainingPageVM result = new Models.TrainingPageVM();

            result.FilterLabel = "Наши направления";

            result.Layout = _layoutRepository.LoadLayout("training");

            IEnumerable<CourseGroupVM> groups = _courseGroupRepository.LoadIntoUI();

            foreach (CourseGroupVM group in groups)
            {
                List<WebSite.Models.TrainingPageVM.CourseCategoryVM> subItems = new List<WebSite.Models.TrainingPageVM.CourseCategoryVM>();
                foreach (CourseVM course in group.Courses.Where(c => c.Order >= 0).OrderBy(c => c.Order))
                {
                    WebSite.Models.TrainingPageVM.CourseCategoryVM cat = new Models.TrainingPageVM.CourseCategoryVM();

                    cat.Name      = course.CourseName;
                    cat.ParamName = course.Route;
                    cat.Selected = false;
                    cat.Course   = course;
                    cat.CustomCSS = course.MenuItemStyle;
                    subItems.Add(cat);
                }

                WebSite.Models.TrainingPageVM.CourseCategoryVM cc = new Models.TrainingPageVM.CourseCategoryVM();
                cc.Name = group.GroupName;
                cc.ParamName = group.RouteName;
                cc.Selected = false;
                cc.ChildCategories = subItems;
                cc.Group           = group;
                cc.CustomCSS        = group.MenuItemStyle;
                result.AddCourceCategory(cc);
            }

            return result;
        }
    }
}